package com.zxl.live.screen.model;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadConnectListener;
import com.liulishuo.filedownloader.FileDownloadQueueSet;
import com.liulishuo.filedownloader.FileDownloadSampleListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.zxl.live.screen.model.bean.DownloadBean;
import com.zxl.live.tools.common.ApplicationHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/3.
 */

public class TasksManager extends FileDownloadSampleListener {

    private Object mLock;

    private List<DownloadBean> mList;

    private List<DownloadBean> mCacheList;

    private static TasksManager sInstance;

    private List<OnDownloadStatusListener> mOnDownloadStatusListener;

    private FileDownloadConnectListener mFileDownloadConnectListener = new FileDownloadConnectListener() {
        @Override
        public void connected() {
            if (mCacheList.isEmpty()) {
                return;
            }
            synchronized (mLock) {
                FileDownloadQueueSet queueSet = new FileDownloadQueueSet(TasksManager.this);
                List<BaseDownloadTask> tasks = new ArrayList<>();
                for (DownloadBean downloadBean : mCacheList) {
                    tasks.add(FileDownloader.getImpl().create(downloadBean.url).setPath(downloadBean.path));
                }
                queueSet.downloadTogether(tasks).start();
                mCacheList.clear();
            }
        }

        @Override
        public void disconnected() {
        }
    };

    private TasksManager() {
        mLock = new Object();
        mCacheList = new ArrayList<>();
        mList = new ArrayList<>();
        FileDownloader.setup(ApplicationHelper.getContext());
        FileDownloader.getImpl().bindService();
        FileDownloader.getImpl().addServiceConnectListener(mFileDownloadConnectListener);
    }

    public static TasksManager getInstance() {
        if (null == sInstance) {
            sInstance = new TasksManager();
        }
        return sInstance;
    }

    @Override
    protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
        if (null != mOnDownloadStatusListener) {
            synchronized (mLock) {
                for (OnDownloadStatusListener onDownloadStatusListener : mOnDownloadStatusListener) {
                    onDownloadStatusListener.onPause(task);
                }
            }
        }
    }

    @Override
    protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
        int progress = (int) (((float) soFarBytes / totalBytes) * 100);
        if (null != mOnDownloadStatusListener) {
            synchronized (mLock) {
                for (OnDownloadStatusListener onDownloadStatusListener : mOnDownloadStatusListener) {
                    onDownloadStatusListener.onProgress(task, progress);
                }
            }
        }
    }

    @Override
    protected void completed(BaseDownloadTask task) {
        synchronized (this) {
            for (DownloadBean downloadBean : mList) {
                if (downloadBean.getId() == task.getId()) {
                    mList.remove(downloadBean);
                    break;
                }
            }
        }
        if (null != mOnDownloadStatusListener) {
            synchronized (mLock) {
                for (OnDownloadStatusListener onDownloadStatusListener : mOnDownloadStatusListener) {
                    onDownloadStatusListener.onCompleted(task);
                }
            }
        }
    }

    @Override
    protected void error(BaseDownloadTask task, Throwable e) {
        synchronized (this) {
            for (DownloadBean downloadBean : mList) {
                if (downloadBean.getId() == task.getId()) {
                    mList.remove(downloadBean);
                    break;
                }
            }
        }
        if (null != mOnDownloadStatusListener) {
            synchronized (mLock) {
                for (OnDownloadStatusListener onDownloadStatusListener : mOnDownloadStatusListener) {
                    onDownloadStatusListener.onError(task);
                }
            }
        }
    }

    public void addOnDownloadStatusListener(OnDownloadStatusListener onDownloadStatusListener) {
        if (null == mOnDownloadStatusListener) {
            mOnDownloadStatusListener = new ArrayList<>();
        }
        if (!mOnDownloadStatusListener.contains(onDownloadStatusListener)) {
            mOnDownloadStatusListener.add(onDownloadStatusListener);
        }
    }

    public void removeOnDownloadStatusListener(OnDownloadStatusListener onDownloadStatusListener) {
        if (null != mOnDownloadStatusListener) {
            mOnDownloadStatusListener.remove(onDownloadStatusListener);
        }
    }

    public void clearOnDownloadStatusListener() {
        if (null != mOnDownloadStatusListener) {
            mOnDownloadStatusListener.clear();
        }
    }

    public void addTask(DownloadBean downloadBean) {
        if (isReady()) {
            FileDownloader.getImpl().create(downloadBean.url).setPath(downloadBean.path).setListener(this).start();
        } else {
            synchronized (mLock) {
                mCacheList.add(downloadBean);
            }
        }
        mList.add(downloadBean);
    }

    public static boolean isReady() {
        return FileDownloader.getImpl().isServiceConnected();
    }

    public void restart(DownloadBean downloadBean) {
        if (isReady()) {
            FileDownloader.getImpl().create(downloadBean.url).setPath(downloadBean.path).setListener(this).start();
        }
    }

    public void pause(DownloadBean downloadBean) {
        if (isReady()) {
            FileDownloader.getImpl().pause(downloadBean.getId());
        }
    }

    public boolean isEmpty() {
        return mList.isEmpty();
    }

    public interface OnDownloadStatusListener {

        void onPause(BaseDownloadTask task);

        void onError(BaseDownloadTask task);

        void onCompleted(BaseDownloadTask task);

        void onProgress(BaseDownloadTask task, int progress);

    }
}
